package com.ks.user.controller;


import com.ks.core.base._ControllerBase;
import com.ks.core.core.Result;
import com.ks.core.dto.SearchCriteriaBase;
import com.ks.user.controller.dto.PowerResurceRequest;
import com.ks.user.controller.dto.PowerRoleRequest;
import com.ks.user.controller.dto.RoleRequest;
import com.ks.user.controller.dto.UserRoleRequest;
import com.ks.user.entity.ApBase;
import com.ks.user.entity.PowerResurce;
import com.ks.user.service.IPowerResurceService;
import com.ks.user.service.IPowerRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author kingdee123
 * @since 2020-11-30
 */
@RestController
@RequestMapping("/powerRole")
@Api(value = "权限管理", description = "权限管理")
public class PowerRoleController extends _ControllerBase {
    @Autowired
    private IPowerRoleService powerRoleService;

    @Autowired
    private IPowerResurceService powerResurceService;


    @PostMapping("/getRoleList")
    @ApiOperation(value = "根据角色Id,查询角色拥有的权限", notes = "根据角色Id,查询角色拥有的权限")
    public Object getRoleList(@RequestBody SearchCriteriaBase searchCriteriaBase) {
        Map<String, Object> roleList = powerResurceService.getRoleList(getContext(), searchCriteriaBase);
        return Result.success(roleList);
    }


    @PostMapping("/addPowerRole")
    @ApiOperation(value = "角色添加接口", notes = "角色添加接口")
    public Object addPowerRole(@RequestBody PowerRoleRequest powerRoleRequest) {
        Map<String, Object> resultMap = powerRoleService.addPowerRole(getContext(), powerRoleRequest);
        return Result.success(resultMap);
    }

    @PostMapping("/updatePowerRole")
    @ApiOperation(value = "角色修改接口", notes = "角色修改接口")
    public Object updatePowerRole(@RequestBody PowerRoleRequest powerRoleRequest) {
        Map<String, Object> resultMap = powerRoleService.updatePowerRole(getContext(), powerRoleRequest);
        return Result.success(resultMap);
    }

    @PostMapping("/selectPowerRole")
    @ApiOperation(value = "角色查询接口", notes = "角色查询接口")
    public Object selectPowerRole(@RequestBody SearchCriteriaBase searchCriteriaBase) {
        Map<String, Object> resultMap = powerRoleService.selectPowerRole(getContext(), searchCriteriaBase);
        return Result.success(resultMap);
    }


    @PostMapping("/deletePowerRole")
    @ApiOperation(value = "角色删除接口", notes = "角色删除接口")
    public Object deletePowerRole(@RequestBody PowerRoleRequest powerRoleRequest) {
        int resultMap = powerRoleService.deletePowerRole(getContext(), powerRoleRequest);
        return Result.success(resultMap);
    }


    @PostMapping("/addPowerRes")
    @ApiOperation(value = "权限资源添加接口", notes = "权限资源添加接口")
    public Object addPowerRes(@RequestBody PowerResurceRequest powerResurceRequest) {
        Map<String, Object> resultMap = powerResurceService.addPowerRes(getContext(), powerResurceRequest);
        return Result.success(resultMap);
    }

    @PostMapping("/updatePowerRes")
    @ApiOperation(value = "权限资源修改接口", notes = "权限资源修改接口")
    public Object updatePowerRes(@RequestBody PowerResurceRequest powerResurceRequest) {
        Map<String, Object> resultMap = powerResurceService.updatePowerRes(getContext(), powerResurceRequest);
        return Result.success(resultMap);
    }

    @PostMapping("/selectPowerRes")
    @ApiOperation(value = "权限资源查询接口", notes = "权限资源查询接口")
    public Object selectPowerRes(@RequestBody SearchCriteriaBase searchCriteriaBase) {
        Map<String, Object> resultMap = powerResurceService.selectPowerRes(getContext(), searchCriteriaBase);
        return Result.success(resultMap);
    }

    @PostMapping("/deletePowerRes")
    @ApiOperation(value = "权限资源删除接口", notes = "权限资源删除接口")
    public Object deletePowerRes(@RequestBody PowerResurceRequest powerResurceRequest) {
        int resultMap = powerResurceService.deletePowerRes(getContext(), powerResurceRequest);
        return Result.success(resultMap);
    }

    @PostMapping("/addRole")
    @ApiOperation(value = "角色授权接口", notes = "角色授权接口")
    public Object addRole(@RequestBody RoleRequest roleRequest) {
        int resultMap = powerRoleService.addRole(getContext(), roleRequest);
        return Result.success(resultMap);
    }


    @PostMapping("/setUserRole")
    @ApiOperation(value = "设置用户角色接口", notes = "设置用户角色接口")
    public Object setUserRole(@RequestBody UserRoleRequest userRoleRequest) {
        int resultMap = powerRoleService.setUserRole(getContext(), userRoleRequest);
        return Result.success(resultMap);
    }


    @PostMapping("/moveRole")
    @ApiOperation(value = "角色权限移除接口", notes = "角色权限移除接口")
    public Object moveRoleList(@RequestBody RoleRequest roleRequest) {
        int i = powerRoleService.moveEmpower(getContext(), roleRequest.getResurceIds(), roleRequest.getRoleId());
        return Result.success(i);
    }
}

